package com.avic.szzx.config;

import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import com.avic.szzx.controller.bean.NebulaProperties;
import com.vesoft.nebula.client.graph.SessionPool;
import com.vesoft.nebula.client.graph.SessionPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
@Slf4j
public class AsyncThreadConfiguration {

    @Bean("kafkaThreadExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//核心线程数
        executor.setMaxPoolSize(50);//最大线程数
        executor.setKeepAliveSeconds(60);//空闲线程存活时间
        executor.setThreadNamePrefix("kafkaThreadAsync-");
        executor.initialize();
        return executor;
    }
    @Bean("sessionPoolExecutor")
    public SessionPool sessionPoolExecutor(NebulaProperties properties) {
        SessionPoolConfig sessionPoolConfig =
                new SessionPoolConfig(properties.getAddress().stream().map(d -> new HostAddress(d.getHost(), d.getPort())).collect(Collectors.toList()), properties.getSpace(), properties.getUsername(), properties.getPassword())
                        .setMaxSessionSize(100)// 连接池最大连接数
                        .setMinSessionSize(10)// 连接池最小连接数
                        .setRetryConnectTimes(3)// 重连次数
                        .setWaitTime(120)
                        .setRetryTimes(3)
                        .setIntervalTime(120);
        SessionPool sessionPool = new SessionPool(sessionPoolConfig);
        return sessionPool;
    }
}